package com.jevons.geo_lib.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.jevons.geo_lib.R;

/**
 * Copyright 2020 JxGIS
 *
 * @file SimpleDialog
 * @auth linaisheng
 * Created on 2024/4/29.
 * Description：
 */
public class SimpleDialog extends AlertDialog {

    TextView dialogTitle;
    TextView dialogMessage;

    TextView btnConfirm;
    TextView btnCancel;

    View viewDivider;


    public SimpleDialog(@NonNull Context context) {
        super(context, R.style.MapDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_simple, null);
        setView(view);

        dialogTitle = view.findViewById(R.id.tv_dialog_title);
        dialogMessage = view.findViewById(R.id.tv_dialog_message);

        btnConfirm = view.findViewById(R.id.btn_confirm);
        btnCancel = view.findViewById(R.id.btn_cancel);
        viewDivider = view.findViewById(R.id.view_alert_divider);

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) dialogMessage.getLayoutParams();
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogMessage.setLayoutParams(layoutParams);

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

    }


    public SimpleDialog setTitle(String title) {
        dialogTitle.setText(title);
        return this;
    }

    public SimpleDialog setMessage(String message) {
        dialogMessage.setText(message);
        return this;
    }

    public SimpleDialog setCharSequenceMessage(CharSequence message) {
        dialogMessage.setText(message);
        return this;
    }

    public SimpleDialog setMessageGravity(int gravity) {
        dialogMessage.setGravity(gravity);
        return this;
    }

    public SimpleDialog setConfirm(String confirmTxt, View.OnClickListener listener) {
        btnConfirm.setText(confirmTxt);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(view);
                }
                dismiss();
            }
        });
        return this;
    }

    public SimpleDialog setCancel(String cancelTxt, View.OnClickListener listener) {
        btnCancel.setVisibility(View.VISIBLE);
        viewDivider.setVisibility(View.VISIBLE);
        btnCancel.setText(cancelTxt);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(view);
                }
                dismiss();
            }
        });
        return this;
    }

    public SimpleDialog setDialogCancelable(boolean cancelable) {
        setCanceledOnTouchOutside(cancelable);
        setCancelable(cancelable);
        return this;
    }

}